package com.road.vast.department.controller;


import com.road.vast.common.entity.Result;
import com.road.vast.common.entity.ResultCode;
import com.road.vast.company.service.CoCompanyService;
import com.road.vast.controller.BaseController;
import com.road.vast.department.service.CoDepartmentService;
import com.road.vast.pojo.company.CoCompany;
import com.road.vast.pojo.company.CoDepartment;
import com.road.vast.response.DeptListResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/company")
public class CoDepartmentController extends BaseController {

    @Autowired
    CoDepartmentService coDepartmentService;

    @Autowired
    private CoCompanyService coCompanyService;

    /**
     * 保存部门信息
     */
    @PostMapping("/department")
    public Result saveDepartment(@RequestBody CoDepartment coDepartment){
        coDepartment.setCompanyId(companyId);
        coDepartmentService.saveDepartment(coDepartment);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 更新部门信息
     */
    @PutMapping("/department/{id}")
    public Result updateDepartment(@PathVariable(value = "id") String id,
                                   @RequestBody CoDepartment coDepartment){
        coDepartment.setId(id);     //设置要修改的部门的id
        coDepartmentService.updateDepartment(coDepartment);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 通过id查询
     */
    @GetMapping("/department/{id}")
    public Result findById(@PathVariable(value = "id") String id){
        CoDepartment department = coDepartmentService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(department);
        return result;
    }

    /**
     * 通过企业id进行查询
     */
    @GetMapping("/department")
    public Result findAll(){
        List<CoDepartment> departments = coDepartmentService.findAll(companyId);
        CoCompany company = coCompanyService.findById(companyId);
        DeptListResult list = new DeptListResult(company, departments);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(list);
        return result;
    }

    /**
     * 根据id删除部门
     */
    @DeleteMapping("/department/{id}")
    public Result deleteCoDepartment(@PathVariable(value = "id") String id){
        coDepartmentService.deleteCoDepartment(id);
        return new Result(ResultCode.SUCCESS);
    }

}

